<?php
namespace Home\Service;

use Think\Service\BasicService;

class ERPAPIService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->find($condition);
    }
    public function GetData($condition,$access_token) {
        $conn = mssql_conn();
        $SUCCESS_NUM = 0;
        $ERROR_NUM = 0;
        $OKS  = 1;
        $this->db->startTrans();
        $OrderData =  $this->db->where($condition)->select();
        foreach($OrderData as $k => $v){
            $where['wyy_djbh'] = $v['wyy_djbh'];
            $save['dr_state'] =1;
            $this->db = M('kx_erp_order');
            $OK = $this->db->where($where)->save($save);
            $OK ? 1:$OKS = 0;
            $this->db = M('kx_erp_order_info');
            $OK = $this->db->where($where)->save($save);
            $OK ? 1:$OKS = 0;
            $sql = "Insert into  wyy_Orders_WMX values('"
                .$v['wyy_djbh']."','"
                .$v['wyy_dwbh']."','".$v['danwbh']."','"
                .$v['wyy_Date']."','".$v['wyy_Agio']."','"
                .$v['wyy_je']."','".$v['wyy_AgioTotal']."','".
                $v['shouhr']."','".$v['shouhdz']."','".$v['tel']."','".
                $v['wyy_In']."','".$v['status']."','".$v['wyy_Note']."','".
                $v['FreePostStatus']."','".$v['FreePostAmount']."','".
                $v['xgdjbh']."','". $v['district']."','". $v['wyy_djbh']
                ."')";
            $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
            $OK = sqlsrv_query( $conn, $sql );
            if($OK){
                $SUCCESS_NUM ++;
            }else{
                $ERROR_NUM ++;
                $OKS = 0;
            }
            $order_info = $this->db->where($where)->select();
            foreach($order_info as $k1=>$v1){
                $sql1 = "Insert into  wyy_OrderDetails_WMX values('"
                    .$v1['wyy_djbh']."','"
                    .$v1['wyy_Sort']."','".$v1['wyyspid']."','"
                    .$v1['spbh']."','".$v1['price']."','"
                    .$v1['dw']."','".$v1['shl']."','"
                    .$v1['wyy_In']."','".$v1['isgift']."','".$v1['wyy_djbh']
                    ."')";
                $sql1 = iconv('utf-8', 'GB2312//IGNORE', $sql1);
                $OK = sqlsrv_query( $conn, $sql1 );
                $OK ? 1:$OKS = 0;
            }

        }
        /*
         * 发货操作
         */
//        $URL = C('FH_URL_ROOT');
//        $OK = curl_file_get_contents($URL,array('tid'=>$v['wyy_djbh'],'access_token'=>$access_token));
//        $OK ? 1:$OKS = 0;
        if($OKS){
            $res = array(
                'code'=>'ok',
                'SUCCESS_NUM'=>$SUCCESS_NUM,
                'ERROR_NUM'=>$ERROR_NUM
            );
            $this->db->commit();
            return $res;
        }else{
            $this->db->rollback();
            $res = array(
                'code'=>0,
                'SUCCESS_NUM'=>$SUCCESS_NUM,
                'ERROR_NUM'=>$ERROR_NUM
            );
            return $res;
        }
    }

    public function query($condition) {
        $data = $this->db->where($condition)->order('wyy_Date desc')->select();
        foreach ($data as $k => $v) {
            $this->db = M('kx_erp_order_info');
            $datas = $this->db->field('t_kx_erp_order_info.*,t_yz_goods.title,t_yz_goods.sku1,t_yz_goods.sku2,t_yz_goods.sku3')
                ->join('t_yz_goods on t_yz_goods.alias = t_kx_erp_order_info.wyyspid')
                ->where(array('wyy_djbh' => $v['wyy_djbh']))->select();
            $data[$k]['goodsdata'] = $datas;
        }
        return $data;
    }

    public function GetzjkOrders($ID){
        $DATA = array();
        $t = 0;
        $conn = mssql_conn();
        $sql = "select * from wyy_Orders_wmx where wyy_djbh = '".$ID."'";
        $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
        $res = sqlsrv_query( $conn, $sql );
        while($row = sqlsrv_fetch_array($res)){
            $DATA[$t] = $row;
            $DATA[$t]['shouhdz'] =iconv('GBK', 'UTF-8', $row['shouhdz']);
            $DATA[$t]['shouhr'] =iconv('GBK', 'UTF-8', $row['shouhr']);

            $sql1 = "select * from wyy_OrderDetails_wmx where wyy_djbh = '".$ID."'";
            $sql1 = iconv('utf-8', 'GB2312//IGNORE', $sql1);
            $res1 = sqlsrv_query( $conn, $sql1 );
            while($row1 = sqlsrv_fetch_array($res1)){
                $DATA[$t]['goodsdata'][] = $row1;
            }

        }
        return $DATA;
    }

}

?>